# -*- encoding: utf-8 -*-
"""
@File : weibo.py
@Time : 2022/5/26 23:31
@Author : Linleil
"""
import requests
import json
import jsonpath
import logging
import datetime
import pytz
from apscheduler.schedulers.blocking import BlockingScheduler


logging.basicConfig(filename='demo.txt', level=logging.INFO, format=' %(asctime)s - %(levelname)s ----- %(message)s')


def get_hot_band():
    # create a file to write hot_band's response
    tz = pytz.timezone('Asia/Shanghai')
    time_str = datetime.datetime.now(tz).strftime("%Y%m%d%H")
    filename = time_str + '.json'

    url = 'https://weibo.com/ajax/statuses/hot_band'
    response = requests.get(url=url)
    response = response.text
    response = json.loads(response)
    # get data.band_list of response
    data = jsonpath.jsonpath(response, 'data.band_list[0]')

    logging.info('Method get_hot_band used success.')
    with open(filename, 'w', encoding='utf-8') as file:
        file.write(json.dumps(data, indent=2, ensure_ascii=False))
    logging.info('write down')


# get_hot_band()

"""create a job to run get_hot_band"""
sched = BlockingScheduler()
sched.add_job(get_hot_band, 'interval', start_date="2022-7-28 2:25:00", end_date="2022-8-28 2:20:00", hours=1, timezone='Asia/Shanghai')
sched.start()
